﻿Imports System.IO

Module GetFiles

    Friend FolderPath, Flag, GameName, GameNameByte, SaveFile, SaveFileByte As String
    Friend FlagFind As Long

    Friend Sub GetSaves()

        Dim i As Integer

        Try
            For Each Singlefile In Directory.GetFiles(FolderPath)

                If Singlefile.Contains(".bak") Then

                Else
                    'put filepath into a string
                    Main.STFSFilePath = Singlefile

                    Try
                        Main.STFSFilePath = Singlefile
                        CheckifCON()
                        If CheckifCON() = True Then



                            Dim exists As Boolean = False
                            Dim count As Integer = 0
                            Do Until count = Main.SavesListView.Items.Count
                                If Main.SavesListView.Items(count).SubItems(2).Text = Singlefile Then
                                    exists = True
                                    Exit For
                                End If
                                count += 1
                            Loop

                            If exists = False Then
                                'get Game name
                                GetGameName()

                                'need to do this, otherwise the Save name would not show correctly
                                GameNameByte = ""

                                '
                                GetFilename()

                                Dim p As Integer
                                Dim sb As New System.Text.StringBuilder()
                                For p = 1 To GameName.Length Step 2
                                    sb.Append(Convert.ToChar(Convert.ToInt32(Mid(GameName, p, 2), 16)))
                                Next p
                                GameName = sb.ToString()

                                Dim l As Integer
                                Dim sc As New System.Text.StringBuilder()
                                For l = 1 To SaveFile.Length Step 2
                                    sc.Append(Convert.ToChar(Convert.ToInt32(Mid(SaveFile, l, 2), 16)))
                                Next l
                                SaveFile = sc.ToString()


                                GetProfileID()
                                GetDeviceID()
                                GetConsoleID()
                                GetTypeFile()

                                Dim w As Integer = Integer.Parse(Main.SavesListView.Items.Count)
                                Main.SavesListView.Items.Add(GameName, w)

                                Main.SavesListView.Items(w).SubItems.Add(Flag)
                                Main.SavesListView.Items(w).SubItems.Add(Singlefile)
                                Main.SavesListView.Items(w).SubItems.Add(SaveFile)  '
                                Main.SavesListView.Items(w).SubItems.Add(ProfileIDRead)
                                Main.SavesListView.Items(w).SubItems.Add(DeviceIDRead)
                                Main.SavesListView.Items(w).SubItems.Add(ConsoleIDRead)
                                Main.SavesListView.Items(w).SubItems.Add(FileType)
                                i += 1

                                'add found item to log
                                Main.LogListBox.Items.Add("   Found " & Main.STFSFilePath.Replace(FolderPath & "\", "") & "...")
                                'select and deselect last item in list so it 'auto scrolls'
                                Main.LogListBox.SelectedIndex = Main.LogListBox.Items.Count - 1
                                Main.LogListBox.SelectedIndex = -1
                                Main.GamesCount += 1
                            End If
                            exists = False
                        Else

                        End If
                    Catch ex As Exception

                    End Try
                End If
            Next

            For Each folder In Directory.GetDirectories(FolderPath)


                For Each subfolder In Directory.GetDirectories(folder)
                    'f = file
                    For Each F In Directory.GetFiles(subfolder)
                        'if file is a *.bak (a backup file) then ignore
                        If F.Contains(".bak") Then

                        Else
                            'put filepath into a string
                            Main.STFSFilePath = F

                            Try
                                Main.STFSFilePath = F
                                CheckifCON()
                                If CheckifCON() = True Then



                                    Dim exists As Boolean = False
                                    Dim count As Integer = 0
                                    Do Until count = Main.SavesListView.Items.Count
                                        If Main.SavesListView.Items(count).SubItems(2).Text = F Then
                                            exists = True
                                            Exit For
                                        End If
                                        count += 1
                                    Loop

                                    If exists = False Then
                                        'get Game name
                                        GetGameName()

                                        'need to do this, otherwise the Save name would not show correctly
                                        GameNameByte = ""

                                        '
                                        GetFilename()




                                        'SaveFile = SaveFile.Replace("""", "")

                                        GetProfileID()
                                        GetDeviceID()
                                        GetConsoleID()
                                        GetTypeFile()

                                        Dim w As Integer = Integer.Parse(Main.SavesListView.Items.Count)
                                        Main.SavesListView.Items.Add(GameName, w)

                                        Main.SavesListView.Items(w).SubItems.Add(Flag)
                                        Main.SavesListView.Items(w).SubItems.Add(F)
                                        Main.SavesListView.Items(w).SubItems.Add(SaveFile)
                                        Main.SavesListView.Items(w).SubItems.Add(ProfileIDRead)
                                        Main.SavesListView.Items(w).SubItems.Add(DeviceIDRead)
                                        Main.SavesListView.Items(w).SubItems.Add(ConsoleIDRead)
                                        Main.SavesListView.Items(w).SubItems.Add(FileType)
                                        i += 1

                                        'add found item to log
                                        Main.LogListBox.Items.Add("   Found " & Main.STFSFilePath.Replace(subfolder & "\", "") & "...")
                                        'select and deselect last item in list so it 'auto scrolls'
                                        Main.LogListBox.SelectedIndex = Main.LogListBox.Items.Count - 1
                                        Main.LogListBox.SelectedIndex = -1
                                        Main.GamesCount += 1
                                    End If
                                    exists = False
                                Else

                                End If

                            Catch ex As Exception

                            End Try

                        End If

                    Next
                Next
            Next
        Catch ex As Exception

        End Try

    End Sub

    Friend Function CheckifCON() '(ByVal IsCON As Boolean)

        Dim byteread As String
        SaveFile = ""
        Dim readbyte As Integer = 0
        Dim offset1 As Integer = 0
        Dim CON As String = ""
        Try
            Dim concheck As BinaryReader = New BinaryReader(File.Open(Main.STFSFilePath, FileMode.Open))
            'check if con
            Do Until readbyte = 3

                concheck.BaseStream.Seek(offset1, SeekOrigin.Begin)
                byteread = Hex(concheck.ReadByte())

                CON = CON & byteread
                offset1 += +1
                readbyte += 1
            Loop

            concheck.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

        If CON = "434F4E" Then
            Return True
        Else
            Return False
        End If

        Return True
    End Function

    Friend Sub RefreshList()
        Dim itemscount As Integer = 0
        Do Until itemscount = Main.SavesListView.Items.Count
            If File.Exists(Main.SavesListView.Items(itemscount).SubItems(2).Text) Then
                Try
                    Dim sr As BinaryReader = New BinaryReader(File.Open(Main.SavesListView.Items(itemscount).SubItems(2).Text, FileMode.Open))
                    sr.BaseStream.Seek(556, SeekOrigin.Begin)
                    FlagFind = System.Net.IPAddress.HostToNetworkOrder(sr.ReadInt64())

                    Flag = Hex(FlagFind)
                    Main.STFSFilePath = Main.SavesListView.Items(itemscount).SubItems(2).Text
                    Main.SavesListView.Items(itemscount).SubItems(1).Text = Flag
                    sr.Close()

                    GetTypeFile()
                    GetConsoleID()
                    GetDeviceID()
                    GetProfileID()

                    Main.SavesListView.Items(itemscount).SubItems(4).Text = ProfileIDRead
                    Main.SavesListView.Items(itemscount).SubItems(5).Text = DeviceIDRead
                    Main.SavesListView.Items(itemscount).SubItems(6).Text = ConsoleIDRead

                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try


            Else

            End If
            itemscount += 1
        Loop
    End Sub
End Module
